package cn.tedu.ivoscm.geofence.service.impl;

import cn.tedu.ivoscm.base.exception.ServiceException;
import cn.tedu.ivoscm.base.response.StatusCode;
import cn.tedu.ivoscm.geofence.mapper.GeofenceMapper;
import cn.tedu.ivoscm.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivoscm.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivoscm.geofence.pojo.entity.Geofence;
import cn.tedu.ivoscm.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivoscm.geofence.service.GeofenceService;
import cn.tedu.ivoscm.user.pojo.entity.User;
import cn.tedu.ivoscm.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivoscm.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivoscm.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleService vehicleService;

    @Override
    public List<GeofenceVO> select(GeofenceQuery gq) {
        log.debug("查询电子围栏列表业务：{}", gq);
        List<GeofenceVO> list = geofenceMapper.select(gq);
        // 为每一个围栏VO补全数据
        for (GeofenceVO gvo : list) {
            VehicleQuery vq = new VehicleQuery();
            vq.setGeofenceId(gvo.getId());
            List<VehicleVO> vehicleList = vehicleService.select(vq);
            gvo.setVehicleList(vehicleList);
            gvo.setTotalNum(vehicleList.size());
            Integer availableNum = 0;
            for (VehicleVO vvo : vehicleList) {
                if ("1".equals(vvo.getStatus())) {
                    availableNum++;
                }
            }
            gvo.setAvailableNum(availableNum);
        }
        return list;
    }

    @Override
    public void save(GeofenceSaveParam gsp) {
        log.debug("保存围栏状态业务：{}", gsp);
        Geofence g = new Geofence();
        BeanUtils.copyProperties(gsp, g);
        if (g.getId() == null) {
            g.setCreateTime(new Date());
            g.setStatus("1");
            geofenceMapper.insert(g);
        } else {
            g.setUpdateTime(new Date());
            geofenceMapper.update(g);
        }
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改围栏状态业务：id: {}, status: {}", id, status);
        Geofence g = new Geofence();
        g.setId(id);
        g.setStatus(status);
        g.setUpdateTime(new Date());
        geofenceMapper.update(g);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("删除电子围栏业务：{}", id);
        VehicleQuery vq = new VehicleQuery();
        vq.setGeofenceId(id);
        // 注入后从业务层调用另一个业务层
        List<VehicleVO> list = vehicleService.select(vq);  // 查询围栏绑定的车辆
        if (list.isEmpty()) {
            geofenceMapper.deleteById(id);
        } else {
            throw new ServiceException(StatusCode.VEHICLES_ALREADY_BIND);
        }
    }
}
